﻿using NewRen.Helpers;
using NewRen.Log4net;
using NewRen.Models;
using NewRen.Models.DataModel;
using NewRen.Models.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace NewRen.Controllers
{
    public class AnecdoteController : BaseController
    {
        //牛人轶事首页
        // GET: /Anecdote/
        //[HttpGet]
        public ActionResult Index()
        {
            try
            {
                var anecdotes = db.Anecdotes.Where(a => a.IsPublish == true).OrderByDescending(a => a.Browses).Take(10).ToList();
                ViewBag.Anecdotes = anecdotes;
                return View();
            }
            catch (Exception ex)
            {
                log.Error(new LogContent("牛人轶事首页出错", HttpHelper.GetIPAddress()), ex);
                return Redirect("/Shared/Info?msg=数据异常，请稍后重试");
            }
        }

        /// <summary>
        /// 分页获取牛人轶事
        /// </summary>
        /// <param name="page"></param>
        /// <returns></returns>
        public ActionResult GetAnecdotes(int page)
        {
            int index = page * 10;
            AjaxModel ajaxModel = new AjaxModel();
            List<Anecdote> anecdotes = new List<Anecdote>();
            List<vAnecdote> _anecdotes = new List<vAnecdote>();
            try
            {
                anecdotes = (from a in db.Anecdotes
                             where a.IsPublish == true
                             orderby a.Time descending
                             select a).Skip(index).Take(10).ToList();

                foreach (var item in anecdotes)
                {
                    _anecdotes.Add(new vAnecdote(item));
                }
                if (_anecdotes.Count > 0)
                {
                    ajaxModel.Data = _anecdotes;
                    ajaxModel.Statu = "ok";
                    ajaxModel.Msg = "获取成功";
                }
                else
                {
                    ajaxModel.Data = _anecdotes;
                    ajaxModel.Statu = "err";
                    ajaxModel.Msg = "已加载完";
                }
            }
            catch (Exception ex)
            {
                ajaxModel.Data = _anecdotes;
                ajaxModel.Statu = "err";
                ajaxModel.Msg = "数据异常";
                log.Error(new LogContent("分页获取牛人轶事出错", Helpers.HttpHelper.GetIPAddress()), ex);
            }
            return Json(ajaxModel, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        ///  展示牛人轶事
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult Show(int id)
        {
            try
            {
                var anecdotes = db.Anecdotes.Where(a => a.IsPublish == true).OrderByDescending(a => a.Browses).Take(10).ToList();
                var anecdote = db.Anecdotes.Find(id);
                anecdote.Browses = anecdote.Browses + 1;
                ViewBag.Anecdotes = anecdotes;
                db.SaveChanges();
                return View(anecdote);
            }
            catch (Exception ex)
            {
                log.Error(new LogContent("展示牛人轶事出错", Helpers.HttpHelper.GetIPAddress()), ex);
                return Redirect("/Shared/Info?msg=牛人轶事不存在，请不要不合理操作");
            }
        }
    }
}